package com.kai.service.system.controller;

import com.kai.service.data.PagingResult;
import com.kai.service.data.Result;
import com.kai.service.data.request.system.CreateResourceRequest;
import com.kai.service.data.request.system.PagingResourceRequest;
import com.kai.service.data.request.system.ResourceRequest;
import com.kai.service.data.request.system.UpdateResourceRequest;
import com.kai.service.data.response.system.ResourceResponse;
import com.kai.service.enums.UserStatusEnum;
import com.kai.service.system.service.IResourceService;
import com.kai.service.system.service.IRoleResourceService;
import com.kai.service.util.ResultResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/system")
@Tag(name = "系统资源")
public class ResourceController {

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private IRoleResourceService roleResourceService;

    /**
     * 分层获取所有资源列表
     *
     * @param resourceRequest
     * @return
     */
    @PostMapping("/resource/hierarchy:getList")
    @Operation(summary = "分层获取所有资源列表")
    public Result<List<ResourceResponse>> listHierarchyResource(@RequestBody ResourceRequest resourceRequest){
        return ResultResponseUtil.success(resourceService.listHierarchyResource(resourceRequest));
    }

    /**
     * 分页获取所有资源
     *
     * @param pagingResourceRequest
     * @return
     */
    @PostMapping("/resource:paging")
    @Operation(summary = "分页获取所有资源")
    public PagingResult<ResourceResponse> pagingResource(@RequestBody PagingResourceRequest pagingResourceRequest){
        return ResultResponseUtil.pageSuccess(resourceService.pagingResource(pagingResourceRequest));
    }

    /**
     * 添加资源
     *
     * @param createResourceRequest
     * @return
     */
    @PostMapping("/resource")
    @Operation(summary = "添加资源")
    public Result<Boolean> createResource(@Valid @RequestBody CreateResourceRequest createResourceRequest){
        return ResultResponseUtil.success(resourceService.createResource(createResourceRequest));
    }

    /**
     * 修改资源
     *
     * @param resourceId
     * @param updateResourceRequest
     * @return
     */
    @PutMapping("/resource/{resourceId}")
    @Operation(summary = "修改资源")
    public Result<Boolean> updateResource(@PathVariable("resourceId") Integer resourceId, @RequestBody UpdateResourceRequest updateResourceRequest){
        return ResultResponseUtil.success(resourceService.updateResource(resourceId, updateResourceRequest));
    }

    /**
     * 根据角色id获取对应资源id
     *
     * @param roleId
     * @return
     */
    @GetMapping("/resourceId/{roleId}")
    @Operation(summary = "根据角色id获取对应权限id")
    public Result<List<Integer>> getResourceIdsByRoleId(@PathVariable("roleId") Integer roleId){
        return ResultResponseUtil.success(roleResourceService.getResourceIdsByRoleId(roleId));
    }

    /**
     * 启用资源
     *
     * @param resourceId
     * @return
     */
    @PutMapping("/resource/{resourceId}:enable")
    @Operation(summary = "启用资源")
    public Result<Boolean> enableResource(@PathVariable("resourceId") Integer resourceId){
        return ResultResponseUtil.success(resourceService.stateSwitch(resourceId, UserStatusEnum.NORMAL.getCode()));
    }

    /**
     * 禁用资源
     *
     * @param resourceId
     * @return
     */
    @PutMapping("/resource/{resourceId}:disabled")
    @Operation(summary = "禁用资源")
    public Result<Boolean> disabledResource(@PathVariable("resourceId") Integer resourceId){
        return ResultResponseUtil.success(resourceService.stateSwitch(resourceId, UserStatusEnum.DEACTIVATE.getCode()));
    }

}
